package com.example.music.config;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 暴露响应头
 */
@Component
public class AddResponseHeaderFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        response.addHeader("X-Frame-Options", "DENY");
        response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
        response.addHeader("Cache-Control", "no-cache='set-cookie'");
        response.addHeader("SaToken", StpUtil.getTokenValue());
        response.addHeader("Access-Control-Expose-headers","SaToken");
        response.addHeader("Access-Control-Allow-headers","SaToken");
        filterChain.doFilter(request, response);
    }
}
